import msg01Dao from "../dao/msgDB_01.js";
import msg02Dao from "../dao/msgDB_02.js";
import msg03Dao from "../dao/msgDB_03.js";
import msg04Dao from "../dao/msgDB_04.js";
import msg05Dao from "../dao/msgDB_05.js";
import msg06Dao from "../dao/msgDB_06.js";
import msg07Dao from "../dao/msgDB_07.js";
import msg08Dao from "../dao/msgDB_08.js";
import msg09Dao from "../dao/msgDB_09.js";
import {getNow} from "../utils/utils.js";

export const getMsgDao = (sessionID) => {
  let dao;
  let num = sessionID % 9 + 1;
  switch (num) {
    case 1:
      dao = msg01Dao;
      break;
    case 2:
      dao = msg02Dao;
      break;
    case 3:
      dao = msg03Dao;
      break;
    case 4:
      dao = msg04Dao;
      break;
    case 5:
      dao = msg05Dao;
      break;
    case 6:
      dao = msg06Dao;
      break;
    case 7:
      dao = msg07Dao;
      break;
    case 8:
      dao = msg08Dao;
      break;
    case 9:
      dao = msg09Dao;
      break;
  }
  return dao;
};
export const saveMsg = async (data) => {
  console.log(data);
  let msgDao = getMsgDao(data.sessionID);
  if (typeof data.message !== 'string') {
    data.message = JSON.stringify(data.message);
  }
  try {
    await msgDao.addUserMsg(data.sessionID, data.sender, data.timestamp, data.message, 0, data.timestamp);
  } catch (e) {
    return false;
  }
  return true;
};
export const updateMsgStatus = async (data) => {
  console.log(data);
  let msgDao = getMsgDao(data.session_id);
  if (!(data.session_id && data.timestamp && data.sender) || data.status) {
    return false;
  }
  try {
    let updateRes = await msgDao.updateUserMsg(data.session_id, data.sender, data.timestamp, data.message, 1, getNow());
    if (updateRes[0] === 0) {
      return false;
    }
  } catch (e) {
    return false;
  }
  return true;
};
export const getMsgBySessionId = async (sessionID, startTime) => {
  let msgDao = getMsgDao(sessionID);
  let msgList = await msgDao.getMessageListBySessionId(sessionID, startTime ?? 0);
  return msgList;
};

